package com.itqf.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2021/1/18
 * @Time: 下午4:09
 */
//public class MyAdvice implements MethodBeforeAdvice {
//}

@Component
@Aspect   //切面（通知+切点）
public  class MyAdvice{

    //切点 xml配置 <aop:pointcut expression="execution(* com.itqf.service.impl.*.*(..))">
    @Pointcut("execution(* com.itqf.service.impl.*.*(..))")
    public  void  myPointcut(){}

    /**
     *
     * @param joinPoint  得到目标类的相关信息 (类信息  方法   参数   。。。)
     */
    @Before("myPointcut()")
    public  void  before(JoinPoint joinPoint){
        //joinPoint.getTarget().getClass()  目标类的字节码对象
        //joinPoint.getSignature().getName()目标方法的方法名
        //Arrays.toString(joinPoint.getArgs()  目标方法参数
        System.out.println("前.........."+joinPoint.getTarget().getClass()+"--"+
        joinPoint.getSignature().getName()+"---"+Arrays.toString(joinPoint.getArgs()));
    }

    @Around("myPointcut()")
    /**
     *  *ProceedingJoinPoint exposes the proceed(..) method in order to support around advice in @AJ aspects
     *
     */
    public   Object arroud(ProceedingJoinPoint proceedingJoinPoint){
        System.out.println("环绕--前..........");
        long start = System.currentTimeMillis();
        Object res = null;
        try {
            //调用目标方法
          res =  proceedingJoinPoint.proceed();

         long end = System.currentTimeMillis();
        String  method = proceedingJoinPoint.getTarget().getClass()+"."+proceedingJoinPoint.getSignature().getName();
         System.out.println("调用"+method+"时间是："+(end-start));

        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }


        System.out.println("环绕--后.........."+res);

        return  res;
    }


    @After("myPointcut()")
    public  void  after(JoinPoint joinPoint){
        System.out.println("后....");//

        System.out.println(joinPoint.getThis().getClass()+"."+joinPoint.getSignature().getName());
        //logService.addLog(log);
    }


    @AfterThrowing(value = "myPointcut()",throwing = "e")
    public  void  afterThrowing(Throwable e){
        //e.printStackTrace();
        System.out.println("异常...."+e.getLocalizedMessage());
    }


    @AfterReturning(value = "myPointcut()")
    public  void afterReturning(){
        System.out.println("AfterReturning.....");
    }


}
